﻿namespace PublicStaticClass
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    public class NumConversion
    {
        private static double Eval(string strRMB)
        {
            try
            {
                if (null == strRMB)
                {
                    return 0.0;
                }
                strRMB = Replace(strRMB, false);
                if ("" == strRMB)
                {
                    return 0.0;
                }
                int exp = 0;
                int num2 = 0;
                double num3 = 0.0;
                for (int i = strRMB.Length - 1; i > -1; i--)
                {
                    char cUnit = strRMB[i];
                    if ((((cUnit == '元') || (cUnit == '万')) || ((cUnit == '亿') || (cUnit == '圆'))) || (cUnit == 0x842c))
                    {
                        exp = GetExp(cUnit);
                        num2 = 0;
                    }
                    else if (Regex.IsMatch(cUnit.ToString(), @"^\d"))
                    {
                        num3 += Convert.ToInt32(cUnit.ToString()) * Math.Pow(10.0, (double) (exp + num2));
                    }
                    else
                    {
                        num2 = GetExp(cUnit);
                    }
                }
                return num3;
            }
            catch
            {
                return -1.0;
            }
        }

        public static string Format(double numRMB)
        {
            try
            {
                if (0.0 == numRMB)
                {
                    return "零元整";
                }
                StringBuilder builder = new StringBuilder();
                ulong num = Convert.ToUInt64((double) (numRMB * 100.0));
                builder.Insert(0, ToUpper(Convert.ToInt32((ulong) (num % ((ulong) 100L))), -2));
                num /= (ulong) 100L;
                for (int i = 0; num != 0L; i += 4)
                {
                    builder.Insert(0, ToUpper(Convert.ToInt32((ulong) (num % ((ulong) 0x2710L))), i));
                    num /= (ulong) 0x2710L;
                }
                return Regex.Replace(builder.ToString(), "零+", "零").Replace("元零整", "元整").Replace("零元", "元").Trim(new char[] { '零' });
            }
            catch
            {
                return "";
            }
        }

        public static double Format(string strRMB)
        {
            try
            {
                if (Regex.IsMatch(strRMB, @"^\d"))
                {
                    strRMB = Regex.Replace(strRMB, "元|圆", "");
                    char cUnit = strRMB[strRMB.Length - 1];
                    switch (cUnit)
                    {
                        case '万':
                        case '萬':
                        case '亿':
                            return (Convert.ToDouble(strRMB.Substring(0, strRMB.Length - 1)) * Math.Pow(10.0, (double) GetExp(cUnit)));
                    }
                    return Convert.ToDouble(strRMB);
                }
                return Eval(strRMB);
            }
            catch
            {
                return -1.0;
            }
        }

        private static int GetExp(char cUnit)
        {
            switch (cUnit)
            {
                case '佰':
                case '百':
                    return 2;

                case '元':
                case '圆':
                    return 0;

                case '分':
                    return -2;

                case '万':
                case '萬':
                    return 4;

                case '亿':
                    return 8;

                case '仟':
                case '千':
                    return 3;

                case '十':
                case '拾':
                    return 1;

                case '角':
                    return -1;
            }
            return 0;
        }

        private static string GetUnit(int iCode)
        {
            switch (iCode)
            {
                case -2:
                    return "分";

                case -1:
                    return "角";

                case 0:
                    return "元";

                case 1:
                    return "拾";

                case 2:
                    return "佰";

                case 3:
                    return "仟";

                case 4:
                    return "萬";

                case 8:
                    return "亿";
            }
            return "";
        }

        private static string Replace(string strRMB, bool toUpper)
        {
            if (toUpper)
            {
                strRMB = strRMB.Replace("0", "零");
                strRMB = strRMB.Replace("1", "壹");
                strRMB = strRMB.Replace("2", "贰");
                strRMB = strRMB.Replace("3", "叁");
                strRMB = strRMB.Replace("4", "肆");
                strRMB = strRMB.Replace("5", "伍");
                strRMB = strRMB.Replace("6", "陆");
                strRMB = strRMB.Replace("7", "柒");
                strRMB = strRMB.Replace("8", "捌");
                strRMB = strRMB.Replace("9", "玖");
                return strRMB;
            }
            strRMB = strRMB.Replace("零", "0");
            strRMB = strRMB.Replace("壹", "1");
            strRMB = strRMB.Replace("贰", "2");
            strRMB = strRMB.Replace("叁", "3");
            strRMB = strRMB.Replace("肆", "4");
            strRMB = strRMB.Replace("伍", "5");
            strRMB = strRMB.Replace("陆", "6");
            strRMB = strRMB.Replace("柒", "7");
            strRMB = strRMB.Replace("捌", "8");
            strRMB = strRMB.Replace("玖", "9");
            return strRMB;
        }

        private static string ToUpper(int numRMB, int iUnit)
        {
            try
            {
                if (0 == numRMB)
                {
                    if (iUnit == -2)
                    {
                        return "整";
                    }
                    if (iUnit == 0)
                    {
                        return "元";
                    }
                    return "零";
                }
                StringBuilder builder = new StringBuilder();
                string str = "";
                if (iUnit == -2)
                {
                    int num = numRMB / 10;
                    int num2 = numRMB % 10;
                    if (num > 0)
                    {
                        builder.Append(num);
                        builder.Append(GetUnit(-1));
                        if (num2 > 0)
                        {
                            builder.Append(num2);
                            builder.Append(GetUnit(-2));
                        }
                    }
                    else
                    {
                        builder.Append(num2);
                        builder.Append(GetUnit(-2));
                    }
                    return Replace(builder.ToString(), true);
                }
                str = numRMB.ToString("0000");
                bool flag = false;
                for (int i = 0; i < str.Length; i++)
                {
                    if ((3 - i) > 0)
                    {
                        if ('0' != str[i])
                        {
                            builder.Append(str[i]);
                            builder.Append(GetUnit(3 - i));
                            flag = false;
                        }
                        else
                        {
                            if (!flag)
                            {
                                builder.Append(str[i]);
                            }
                            flag = true;
                        }
                    }
                    else if ('0' != str[i])
                    {
                        builder.Append(str[i]);
                        builder.Append(GetUnit(iUnit));
                        flag = false;
                    }
                    else if (flag)
                    {
                        builder.Insert(builder.Length - 1, GetUnit(iUnit));
                    }
                    else
                    {
                        builder.Append(GetUnit(iUnit));
                        builder.Append(str[i]);
                    }
                }
                return Replace(builder.ToString(), true);
            }
            catch
            {
                return "";
            }
        }
    }
}

